package com.TextFormat;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import com.Managers.FragmentManagerClass;
import com.shareup.R;


public class UtilFonts {

    // This function has whole logic for chips generate
    public static void setChips(MultiAutoCompleteTextView tag) {

        SetChipsBack sback = new SetChipsBack(tag);
        sback.execute();
    }

    public static void setTextBoldFont(TextView text) {

        String from = "font/cb.ttf";
        CreateFromAssets cas = new CreateFromAssets(from, text);
        cas.execute();
    }

    public static void setTextRegularFont(TextView text) {

        String from = "font/cr.ttf";
        CreateFromAssets cas = new CreateFromAssets(from, text);
        cas.execute();
    }

    public static void setTextRegularFont(TextView... view) {

        String from = "font/cr.ttf";
        CreateFromAssets cas = new CreateFromAssets(from, view);
        cas.execute();
    }

    public static void setTextRegularFont(TextView text, boolean italic) {

        String from = "font/cr.ttf";
        if (italic) {
            from = "font/ci.ttf";
        }
        CreateFromAssets cas = new CreateFromAssets(from, text);
        cas.execute();
    }

    public static void setTextTitle(TextView text) {

        String from = "font/honey.ttf";
        CreateFromAssets cas = new CreateFromAssets(from, text);
        cas.execute();
    }

    //
    private static class CreateFromAssets extends AsyncTask<String, String, Typeface> {

        private Context context;

        private String from;

        private TextView text;

        private TextView[] viewGroup;

        public CreateFromAssets(String from, TextView text) {

            super();
            this.from = from;
            this.text = text;
            this.context = text.getContext();
            // text.setVisibility(View.INVISIBLE);
        }

        public CreateFromAssets(String from, TextView[] view) {

            super();
            this.from = from;
            this.viewGroup = view;
            this.context = view[0].getContext();
            // text.setVisibility(View.INVISIBLE);
        }

        @Override
        protected Typeface doInBackground(String... params) {

            return Typeface.createFromAsset(context.getAssets(), from);
        }

        @Override
        protected void onPostExecute(Typeface result) {

            if (viewGroup != null) {
                for (int i = 0; i < viewGroup.length; i++) {

                    TextView v = viewGroup[i];
                    v.setTypeface(result);
                }
            }
            else
                if (text.getTypeface() != result) {
                    text.setTypeface(result);

                }

        }
    }

    private static class SetChipsBack extends AsyncTask<String, String, SpannableStringBuilder> {

        private final char separator = ',';

        private MultiAutoCompleteTextView tagText;

        public SetChipsBack(MultiAutoCompleteTextView tagText) {

            super();
            this.tagText = tagText;
        }

        @Override
        protected SpannableStringBuilder doInBackground(String... arg0) {

            // check comman in string
            String stext = tagText.getText().toString();
            //
            if (stext.contains(String.valueOf(separator))
                        && stext.length() > 0) {
                SpannableStringBuilder ssb = new SpannableStringBuilder(stext);
                // split string wich comma
                String chips[] = stext.trim().split(String.valueOf(separator));
                int x = 0;
                // loop will generate ImageSpan for every country name separated
                // by
                // comma
                for (String c : chips) {
                    // inflate chips_edittext layout
                    LayoutInflater lf = (LayoutInflater) tagText.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    TextView textView = (TextView) lf.inflate(R.layout.tag_item, null);
                    //
                    UtilFonts.setTextBoldFont(textView);
                    textView.setText(c.trim()); // set text
                    // capture bitmapt of genreated textview
                    int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                    textView.measure(spec, spec);
                    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
                    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(b);
                    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
                    textView.draw(canvas);
                    textView.setDrawingCacheEnabled(true);
                    Bitmap cacheBmp = textView.getDrawingCache();
                    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
                    textView.destroyDrawingCache(); // destory drawable
                    // create bitmap drawable for imagespan
                    @SuppressWarnings("deprecation")
                    BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
                    bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight());
                    // create and set imagespan
                    ssb.setSpan(new ImageSpan(bmpDrawable), x, x
                                + c.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    x = x + c.length() + 1;
                }
                return ssb;
            }
            return null;
        }

        @Override
        protected void onPostExecute(SpannableStringBuilder result) {

            if (result != null) {
                // set chips span
                tagText.setText(result);
                // move cursor to last
                tagText.setSelection(tagText.getText().length());
            }
        }
    }

    static class MyTagClick implements OnClickListener {

        private String myName;

        public MyTagClick(String myName) {

            super();
            this.myName = myName;
        }

        @Override
        public void onClick(View v) {

            if (FragmentManagerClass.aExplore != null) {
                FragmentManagerClass.aExplore.loadFromTags(myName);
            }

        }
    }
}
